package com.hudilao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hudilao.common.R;
import com.hudilao.entity.Category;
import com.hudilao.entity.Employee;
import com.hudilao.mapper.CategoryMapper;
import com.hudilao.service.CategoryService;
import com.hudilao.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 管理端：新增分类
     * @param category
     * @return 新增分类结果
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Category category){

        log.info("category:{}",category);

        categoryService.save(category);

        return R.success("新增分类成功");
    }


    /**
     * 管理端：分页查询
     * @param page 当前页码
     * @param pageSize 每页记录数
     * @param name 分类名称，可以为空
     * @return 分页结果
     */
    @GetMapping("/page")
    public R<Page<Category>> page(int page, int pageSize, String name){

        // 分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);

        // 条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        //根据username查询
        if (name != null) {
            queryWrapper.like(Category::getName, name);
        }

        // 添加排序条件，根据type进行排序，sort降序，createTime降序
        queryWrapper.orderByAsc(Category::getType)
                .orderByDesc(Category::getCreateTime);

        // 分页查询
        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }



    /**
     * 管理端：根据id删除分类
     * @param id
     * @return 删除分类结果
     */
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id){

        log.info("删除分类，id为：{}",id);

        //categoryService.removeById(id);
        //categoryService.remove(id);

        return  categoryService.remove(id);
    }


    /**
     * 管理端：根据id修改分类信息
     * @param category
     * @return 修改分类结果
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Category category){

        log.info("修改分类信息：{}",category);

        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }

    /**
     * 管理端：查询分类数据
     * @param type 1为菜品分类，2为套餐分类，根据不同情况展示分类信息
     * @return 查询结果
     */
    @GetMapping("/list/{type}")
    public R<List<Category>> list(@PathVariable int type){

        log.info("查询分类数据");

        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        //log.info(type);

        //添加条件
        queryWrapper.eq(Category::getType,type);
        //log.info(category.getType().toString());

        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);

        //查询
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 查询菜品分类
     * @return
     */
    @GetMapping("/user/list")
    public R<List<CategoryVO>> categoryList() {

        // 查询所有分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getType); // 按照 type 字段进行排序

        List<Category> categories = categoryMapper.selectList(queryWrapper);
        List<CategoryVO> categoryVOS = categories.stream().map(category -> {
            CategoryVO response = new CategoryVO();
            response.setCategoryId(category.getId());
            response.setType(category.getType());
            response.setName(category.getName());
            return response;
        }).collect(Collectors.toList());

        return R.success(categoryVOS);
    }

    /**
     * 管理端：根据id查询类别信息，主要用于修改时的页面回显
     * @param id 类别id
     * @return 查询结果
     */
    @GetMapping("/edit/{id}")
    public R<Category> getById(@PathVariable Long id) {

        //根据id查询员工信息
        return categoryService.getCategoryById(id);
    }

}
